// pages/accountDetails/index.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 2,
    balance: [],
    balancePage: 0,
    balanceAll: 0,
    cash: [],
    cashPage: 0,
    cashAll: 0,
    integral: [],
    integralPage: 0,
    integralAll: 0,
    userDesc: '',
    isNull: false,
  },
  // 跳转转让界面
  toTransfer() {
    wx.navigateTo({
      url: '/pages/transfer/index'
    })
  },

  // 跳转到提现界面
  toWithdraw() {
    wx.navigateTo({
      url: '/pages/withdraw/index'
    })
  },

  changeActive: function(e) {
    this.setData({
      num: e.target.dataset.num
    })
    console.log(e.target.dataset.num == 2 && this.data.balance.length===0)
    if (e.target.dataset.num == 2 && this.data.balance.length===0) {
      this.setData({
        isNull: true
      })
      return false;
    } 
    if (e.target.dataset.num == 1 && this.data.integral.length === 0) {
      this.setData({
        isNull: true
      })
      return false;
    } 
    if (e.target.dataset.num == 3 && this.data.cash.length === 0) {
      this.setData({
        isNull: true
      })
      return false;
    } 
    this.setData({
      isNull: false
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.userInfo = util.isLogin();
    this.init();
    this.init2();
    this.cash();
  },
  init() {
    util.http({
      url: 'api/miniwechat/center/integral_change',
      data: {
        user_id: this.data.userInfo.user_id,
        settle_unit: 1,
        offset: this.data.balancePage,
        limit: 10
      }
    }).then((data) => {
      if (this.data.balancePage==0) {
        this.setData({
          userDesc: data.user_info
        })
      }
      if (data.rows) {
        this.setData({
          isNull: false
        })
        data.rows.forEach(item => {
          switch (item.settle_type) {
            case 1:
              item.settle_type = '后台订单结算'
              break;
            case 2:
              item.settle_type = '前台订单结算'
              break;
            case 3:
              item.settle_type = '一级推荐奖励'
              break;
            case 4:
              item.settle_type = '二级推荐奖励'
              break;
            case 5:
              item.settle_type = '成交奖励'
              break;
            case 6:
              item.settle_type = '成交奖励'
              break;
          }
          switch (item.settle_status) {
            case 1:
              item.settle_status = '新建'
              break;
            case 2:
              item.settle_status = '待结算'
              break;
            case 3:
              item.settle_status = '已结算'
              break;
            case 4:
              item.settle_status = '取消'
              break;
          }
          this.data.balance.push(item);
        })
        this.setData({
          balance: this.data.balance
        })
        this.data.balanceAll = data.total
      } else {
        this.setData({
          isNull: true
        })
      }
      console.log(data)
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },
  init2() {
    util.http({
      url: 'api/miniwechat/center/integral_change',
      data: {
        user_id: this.data.userInfo.user_id,
        settle_unit: 2,
        offset: this.data.integralPage,
        limit: 10
      }
    }).then((data) => {
      if (data.rows) {
        this.setData({
          isNull: false
        })
        data.rows.forEach(item => {
          switch (item.settle_type) {
            case 1:
              item.settle_type = '后台订单结算'
              break;
            case 2:
              item.settle_type = '前台订单结算'
              break;
            case 3:
              item.settle_type = '一级推荐奖励'
              break;
            case 4:
              item.settle_type = '二级推荐奖励'
              break;
            case 5:
              item.settle_type = '成交奖励'
              break;
            case 6:
              item.settle_type = '成交奖励'
              break;
          }
          switch (item.settle_status) {
            case 1:
              item.settle_status = '新建'
              break;
            case 2:
              item.settle_status = '待结算'
              break;
            case 3:
              item.settle_status = '已结算'
              break;
            case 4:
              item.settle_status = '取消'
              break;
          }
          this.data.integral.push(item);
        })
        this.setData({
          integral: this.data.integral
        })
        this.data.integralAll = data.total
      }
      console.log(data)
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },
  cash() {
    util.http({
      url: '/api/miniwechat/center/balance_change',
      data: {
        user_id: this.data.userInfo.user_id,
        offset: this.data.cashPage,
        limit: 10
      }
    }).then((data) => {
      if (data.rows) {
        this.setData({
          isNull: false
        })
        data.rows.forEach(item => {
          switch (item.change_type) {
            case 1:
              item.change_type = '余额支付'
              break;
            case 2:
              item.change_type = '余额退还'
              break;
            case 3:
              item.change_type = '余额解冻'
              break;
            case 4:
              item.change_type = '余额提现'
              break;
            case 5:
              item.change_type = '余额充值'
              break;
            case 6:
              item.change_type = '积分抵扣'
              item.
              break;
            case 7:
              item.change_type = '积分退还'
              break;
            case 8:
              item.change_type = '积分解冻'
              break;
            case 9:
              item.change_type = '积分转让'
              break;
            case 10:
              item.change_type = '积分转入'
              break;
            case 11:
              item.change_type = '线上支付'
              break;
            case 12:
              item.change_type = '线上退还'
              break;
            case 13:
              item.change_type = '余额抵扣'
              break;
          }
          switch (item.change_result) {
            case 1:
              item.change_result = '成功'
              break;
            case 2:
              item.change_result = '失败'
              break;
            case -1:
              item.change_result = '待审核'
              break;
          }
          this.data.cash.push(item);
        })
        this.setData({
          cash: this.data.cash
        })
        this.data.cashAll = data.total
      }
      console.log(data)
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },
  scrollTolower() {
    if (this.data.num == 2) {
      if (this.data.balancePage * 10 >= this.data.balanceAll) {
        return false;
      }
      this.data.balancePage += 1;
      this.init();
    } else if (this.data.num == 3) {
      if (this.data.cashPage * 10 >= this.data.cashAll) {
        return false;
      }
      this.data.cashPage += 1;
      this.cash();
    } else if (this.data.num == 1) {
      if (this.data.integralPage * 10 >= this.data.integralAll) {
        return false;
      }
      this.data.integralPage += 1;
      this.init2();
    }
  }

})